package com.ruoyi.system.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.vo.DistrictVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.SysDistrict;
import com.ruoyi.system.service.ISysDistrictService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 四级地域Controller
 *
 * @author MagicCube
 * @date 2023-05-10
 */
@RestController
@RequestMapping("/sysdistrict")
public class SysDistrictController extends BaseController
{
    @Autowired
    private ISysDistrictService sysDistrictService;

    /**
     * 查询四级地域列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysDistrict sysDistrict)
    {
        startPage();
        List<SysDistrict> list = sysDistrictService.selectSysDistrictList(sysDistrict);
        return getDataTable(list);
    }

    /**
     * 查询省份城市信息
     */
    @GetMapping("/cityList/all")
    public AjaxResult allList(SysDistrict sysDistrict)
    {
        Map<Long, List<DistrictVo>> list = sysDistrictService.selectAllDistrictList(sysDistrict);
        return AjaxResult.success(list);
    }
    /**
     * 二级地域联级查询（省市）
     */
    @GetMapping("/cascadeQuery")
    public AjaxResult cascadeQuery(){
        Map<Long, List<DistrictVo>> list = sysDistrictService.cascadeQuery();
        return AjaxResult.success(list);
    }

    Long[] cityIds = new Long[]{10L,13L,31L,34L};
    /**
     * 查询省份城市列表
     */
    @GetMapping("/cityList/{pid}")
    public AjaxResult provinceList(@PathVariable("pid") Long pid){
//        if (district.getPid()==null||district.getPid()<0){
//            district.setPid(0L);
//        }
        List<SysDistrict> list = new ArrayList<>();
        SysDistrict sysDistrict = new SysDistrict();
        sysDistrict.setPid(pid);
        if (Arrays.asList(cityIds).contains(pid)){
            for (SysDistrict district : sysDistrictService.selectSysDistrictList(sysDistrict)) {
                sysDistrict.setPid(district.getId());
                list.addAll(sysDistrictService.selectSysDistrictList(sysDistrict));
            }
        } else {
            list.addAll(sysDistrictService.selectSysDistrictList(sysDistrict));
        }
        return AjaxResult.success(list);
    }

    /**
     * 查询省份城市列表
     */
    @GetMapping("/cityList")
    public AjaxResult provinceList(SysDistrict district){
        if (district.getPid()==null||district.getPid()<0){
            district.setPid(0L);
        }
        List<SysDistrict> list = sysDistrictService.selectSysDistrictList(district);
        return AjaxResult.success(list);
    }

    @GetMapping("/citys/{pid}")
    public AjaxResult cityList(@PathVariable("pid") Long pid){
        SysDistrict sysDistrict = new SysDistrict();
        sysDistrict.setPid(pid);
        Map<String, List<SysDistrict>> map = sysDistrictService.selectDistrictList(sysDistrict);
        return AjaxResult.success(map);
    }
    /**
     * 导出四级地域列表
     */
    @RequiresPermissions("system:sysdistrict:export")
    @Log(title = "四级地域", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDistrict sysDistrict)
    {
        List<SysDistrict> list = sysDistrictService.selectSysDistrictList(sysDistrict);
        ExcelUtil<SysDistrict> util = new ExcelUtil<SysDistrict>(SysDistrict.class);
        util.exportExcel(response, list, "四级地域数据");
    }

    /**
     * 获取四级地域详细信息
     */
    @RequiresPermissions("system:sysdistrict:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysDistrictService.selectSysDistrictById(id));
    }

    @GetMapping(value = "/cityByName")
    public AjaxResult cityByName(@RequestParam("cityName") String cityName)
    {
        return success(sysDistrictService.selectSysDistrictByName(cityName));
    }

    /**
     * 新增四级地域
     */
    @RequiresPermissions("system:sysdistrict:add")
    @Log(title = "四级地域", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysDistrict sysDistrict)
    {
        return toAjax(sysDistrictService.insertSysDistrict(sysDistrict));
    }

    /**
     * 修改四级地域
     */
    @RequiresPermissions("system:sysdistrict:edit")
    @Log(title = "四级地域", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysDistrict sysDistrict)
    {
        return toAjax(sysDistrictService.updateSysDistrict(sysDistrict));
    }

    /**
     * 删除四级地域
     */
    @RequiresPermissions("system:sysdistrict:remove")
    @Log(title = "四级地域", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysDistrictService.deleteSysDistrictByIds(ids));
    }
}
